package com.atguigu.gmall.common.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
public class ThreadPoolConfig {
    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        //获取服务器核数
        int processors = Runtime.getRuntime().availableProcessors();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                processors * 2,
                processors * 2,
                0,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(20),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()//默认的  抛弃线程 并抛出异常
//                new ThreadPoolExecutor.CallerRunsPolicy()//由执行该线程的线程执行
//                new ThreadPoolExecutor.DiscardOldestPolicy()//抛弃最早进入队列的线程
//        new ThreadPoolExecutor.DiscardPolicy()//不抛出异常 并且丢弃线程
        );
        return threadPoolExecutor;
    }
}
